<?php
/**
 * RUBIK IT Solution & Development ltd.
 *
 * PROJECT		: Rubik Ultimate
 * @version 	: 1.1
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * @author			hoadn <hoadn@rubik.com.vn>
 * @package		 	core.html.filedInfo
 *
 * Created on	:Sep 19, 2008
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls('html.fieldInfo.BasicHtmlFieldInfo');

class BasicFileHtmlFieldInfo extends BasicHtmlFieldInfo {
	
	var $_fileTypes;
	var $_uploadPath = null;
	var $_supportURL = false;
	var $_hasUpdate = false;
	
	/**
	 * Set the control support upload directly URL
	 *
	 * @param boolean $bool
	 */
	function setSupportURL($bool = true) {
		$this->_supportURL = $bool;
	}
	
	/**
	 * The list file type that supported
	 *
	 * @param string $fileTypeList '*.jpg, *.zip, *.pdf'
	 */
	function setSupportedFilteType($fileTypeList) {
		$this->_fileTypes = $fileTypeList;
	}
	
	/**
	 * Function for geting the html for previewing data
	 *
	 * @return string
	 */
	function buildHtmlPreviewData($extAttributes = '', $key=null, $dataType ='S') {
		$link = $this->getDownloadableLink();
		$lbl = $this->getFileName();
		$htmlData = "<a href=\"{$link}\">{$lbl}</a>";
		return $htmlData;
	}
	
	/**
	 * Get the link that can be download
	 *
	 * @return string
	 */
	function getDownloadableLink() {
		$link = $this->getValue();
		if (empty($link)) {
			return "#";
		}
		if (!(strpos($link, 'http') === 0 || strpos($link, '/') === 0)) {
			$link = SYS_ROOT_URL.$link;
		}
		return $link;
	}
	
	/**
	 * Get the file name
	 *
	 */
	function getFileName() {
		$link = $this->getValue();
		$fileName = substr($link, RStringHelper::lastIndexOf($link, '/'));
		if (empty($fileName)) {
			$fileName = substr($link, RStringHelper::lastIndexOf($link, '\\'));
		}
		return $fileName;
	}
	
	/**
	 * For checking if we need to create the filter header
	 * NOTE: please override in case special treat
	 *
	 * @return boolean
	 */
	function shouldCreateFilter() {
		return false;
	}
	
	/**
	 * Function for geting the html for inputing data
	 *
	 * @abstract 
	 * @return string
	 */
	function getHtmlInputData($dataType = '', $extAttribute = '') {
		RCoreImporter::cls('html.BasicHtmlUploadControl');
		$ctrl = new BasicHtmlUploadControl($this->_fieldName, '' , $this->_value, $this->_fileTypes, $this->_supportURL);
		$inputHtml = $ctrl->createUploadFormHtml($extAttribute);
		return $inputHtml;
	}

	/**
	 * Get the uploaded file value
	 *
	 * @param string $pathToUpload
	 * @param array $arrayPost
	 * @return string
	 */
	function getUploadedFilePath($pathToUpload, $arrayPost = null) {
		if (empty($arrayPost)) {
			$arrayPost = $_POST;
		}
		if ($arrayPost['ctrl_rm_'.$this->_fieldName]) {
			return '';
		}
		RCoreImporter::cls('html.BasicHtmlUploadControl');
		$ctrl = new BasicHtmlUploadControl($this->_fieldName, $pathToUpload , $arrayPost, $this->_fileTypes, $this->_supportURL);
		$this->_hasUpdate = $ctrl->hasUpdate();
		return $ctrl->getValueWithoutUrl();
	}
	
	/**
	 * Check if the content has update from the original or not!
	 * @return boolean
	 */
	function hasUpdate() {
		return $this->_hasUpdate;
	}
	
}
?>
